<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountCard.php 2018-05-24 18:17 likexin
   
*/

namespace common\library\wechat\officialaccount;

use common\library\wechat\WechatApi;
use common\component\wechat\Wechat;

class OfficialAccountCard extends WechatApi
{

    /**
     * 获取卡券颜色
     * @return mixed
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function getColors()
    {
        $result = self::getInstance()->card->colors();
        return Wechat::apiError($result);
    }

    /**
     * 卡券开放类目查询
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function getCategories()
    {
        $result = self::getInstance()->card->categories();
        return Wechat::apiError($result);
    }

    /**
     * 创建卡券
     * @param string $cardType 卡类型 [member_card, GROUPON]
     * @param array $attributes 卡信息 详细参数见下方文档地址
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025056
     */
    public static function create(string $cardType = 'member_card', array $attributes)
    {
        $result = self::getInstance()->card->create($cardType, $attributes);
        return Wechat::apiError($result);
    }

    /**
     * 更改卡券信息接口
     * @param string $cardId 卡号
     * @param string $cardType 卡类型
     * @param array $attributes 卡信息 详细参数见下方文档地址
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141229
     */
    public static function update(string $cardId, string $cardType, array $attributes)
    {
        $result = self::getInstance()->card->update($cardId, $cardType, $attributes);
        return Wechat::apiError($result);
    }

    /**
     * 获取卡券详情
     * @param string $cardId 卡号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function get(string $cardId)
    {
        $result = self::getInstance()->card->get($cardId);
        return Wechat::apiError($result);
    }

    /**
     * 批量查询卡列表
     * @param int $offset 查询卡列表的起始偏移量, 从0开始
     * @param int $count 需要查询的卡片的数量
     * @param string $statusList 支持开发者拉出指定状态的卡券列表 [CARD_STATUS_NOT_VERIFY: 待审核, CARD_STATUS_VERIFY_FAIL: 审核失败, CARD_STATUS_VERIFY_OK: 通过审核, CARD_STATUS_USER_DELETE: 卡券被商户删除, CARD_STATUS_DISPATCH: 在公众平台投放过的卡券]
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function getList(int $offset = 0, int $count = 10, string $statusList)
    {
        $result = self::getInstance()->card->list($offset, $count, $statusList);
        return Wechat::apiError($result);
    }

    /**
     * 删除卡券
     * @param string $cardId 卡号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function delete(string $cardId)
    {
        $result = self::getInstance()->card->delete($cardId);
        return Wechat::apiError($result);
    }

    /**
     * 创建二维码
     * @param array $cards
     * @return array|bool|void
     * @others 发者可调用该接口生成一张卡券二维码供用户扫码后添加卡券到卡包
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function createQrCode(array $cards)
    {
        $result = self::getInstance()->card->createQrCode($cards);
        return Wechat::apiError($result);
    }

    /**
     * ticket 换取二维码链接
     * @param string $ticket 获取的二维码 ticket, 凭借此 ticket 可以在有效时间内换取二维码
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function getQrCodeUrl(string $ticket)
    {
        $result = self::getInstance()->card->getQrCodeUrl($ticket);
        return Wechat::apiError($result);
    }

    /**
     * 创建货架接口
     * @param string $banner 页面的banner图
     * @param string $pageTitle 页面的title
     * @param bool $canShare 页面是不是可以分享
     * @param string $scene 投放页面的场景值[SCENE_NEAR_BY: 附近, SCENE_MENU: 自定义菜单, SCENE_QRCODE: 二维码, SCENE_ARTICLE: 公众号文章, SCENE_H5: H5页面, SCENE_IVR: 自动回复, SCENE_CARD_CUSTOM_CELL: 卡券自定义cell]
     * @param array $cardList 卡券列表, 每个元素有两个字段
     * @param array $cardList[][card_id] 卡券ID
     * @param array $cardList[][thumb_url] 封面图
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function createLandingPage(string $banner, string $pageTitle, bool $canShare, string $scene, array $cardList)
    {
        $result = self::getInstance()->card->createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList);
        return Wechat::apiError($result);
    }

    /**
     * 图文消息群发卡券
     * @param string $cardId 卡号
     * @return array|bool|void
     */
    public static function getHtml(string $cardId)
    {
        $result = self::getInstance()->card->getHtml($cardId);
        return Wechat::apiError($result);
    }

    /**
     * 设置测试白名单
     * @param array $openids 目标用户OPENIDS
     * @return array|bool|void
     */
    public static function setTestWhitelist(array $openids)
    {
        $result = self::getInstance()->card->setTestWhitelist($openids);
        return Wechat::apiError($result);
    }

    /**
     * 设置测试白名单(微信号)
     * @param array $usernames 目标用户微信号
     * @return array|bool|void
     */
    public static function setTestWhitelistByName(array $usernames)
    {
        $result = self::getInstance()->card->setTestWhitelistByName($usernames);
        return Wechat::apiError($result);
    }

    /**
     * 获取用户已领取卡券接口
     * @param string $openid 目标用户OPENID
     * @param string $cardId 卡券ID 不填写时默认查询当前 appid 下的卡券
     * @others 用于获取用户卡包里的, 属于该appid下所有可用卡券, 包括正常状态和未生效状态
     * @return array|bool|void
     */
    public static function getUserCards(string $openid, string $cardId = '')
    {
        $result = self::getInstance()->card->getUserCards($openid, $cardId);
        return Wechat::apiError($result);
    }

    /**
     * 设置微信买单接口
     * @param string $cardId 卡券ID
     * @param bool $isOpen 是否开启买单功能
     * @return array|bool|void
     * @other 设置买单的 card_id 必须已经配置了门店, 否则会报错
     */
    public static function setPayCell(string $cardId, bool $isOpen = true)
    {
        $result = self::getInstance()->card->setPayCell($cardId, $isOpen);
        return Wechat::apiError($result);
    }

    /**
     * 增加库存
     * @param string $cardId 卡券ID
     * @param int $amount 增加数量
     * @return array|bool|void
     */
    public static function increaseStock(string $cardId, int $amount)
    {
        $result = self::getInstance()->card->increaseStock($cardId, $amount);
        return Wechat::apiError($result);
    }

    /**
     * 减少库存
     * @param string $cardId 卡券ID
     * @param int $amount 减少数量
     * @return array|bool|void
     */
    public static function reductStock(string $cardId, int $amount)
    {
        $result = self::getInstance()->card->reductStock($cardId, $amount);
        return Wechat::apiError($result);
    }

    /**
     * 导入卡券code
     * @param string $cardId 要导入code的卡券ID
     * @param array $codes  要导入微信卡券后台的自定义 code, 最多100个
     * @return array|bool|void
     */
    public static function codeDeposit(string $cardId, array $codes)
    {
        $result = self::getInstance()->card->code->deposit($cardId, $codes);
        return Wechat::apiError($result);
    }

    /**
     * 查询导入code数目
     * @param string $cardId 要导入code的卡券 ID
     * @return array|bool|void
     */
    public static function codeGetDepositedCount(string $cardId)
    {
        $result = self::getInstance()->card->code->getDepositedCount($cardId);
        return Wechat::apiError($result);
    }

    /**
     * 核查卡券code
     * @param string $cardId
     * @param array $codes
     * @others 为了避免出现导入差错, 强烈建议开发者在查询完 code 数目的时候核查 code 接口校验 code 导入微信后台的情况
     * @return array|bool|void
     */
    public static function codeCheck(string $cardId, array $codes)
    {
        $result = self::getInstance()->card->code->check($cardId, $codes);
        return Wechat::apiError($result);
    }

    /**
     * 查询卡券code
     * @param string $code CODE
     * @param string $cardId 卡券ID
     * @param bool $checkConsume 是否校验code核销状态
     * @return array|bool|void
     */
    public static function codeGet(string $code, string $cardId, bool $checkConsume = true)
    {
        $result = self::getInstance()->card->code->get($code, $cardId, $checkConsume);
        return Wechat::apiError($result);
    }

    /**
     * 核销卡券code
     * @param string $code
     * @param string $cardId
     * @return array|bool|void
     */
    public static function codeConsume(string $code, string $cardId)
    {
        $result = self::getInstance()->code->consume($code, $cardId);
        return Wechat::apiError($result);
    }

    /**
     * 卡券code解码
     * @param string $encryptedCode
     * @return array|bool|void
     */
    public static function codeDecrypt(string $encryptedCode)
    {
        $result = self::getInstance()->code->decrypt($encryptedCode);
        return Wechat::apiError($result);
    }

    /**
     * 更改卡券code
     * @param string $code 变更前code
     * @param string $newCode 变更后code
     * @param string $cardId 卡券ID
     * @return array|bool|void
     */
    public static function codeUpdate(string $code, string $newCode, string $cardId)
    {
        $result = self::getInstance()->code->update($code, $newCode, $cardId);
        return Wechat::apiError($result);
    }

    /**
     * 设置卡券失效
     * @param string $code CODE
     * @param string $cardId 卡券ID
     * @return array|bool|void
     */
    public static function codeDisable(string $code, string $cardId)
    {
        $result = self::getInstance()->card->update($code, $cardId);
        return Wechat::apiError($result);
    }

    /**
     * 通用卡 - 卡券激活
     * @param array $info 激活时信息
     * @param array $info[] TODO
     * @return array|bool|void
     */
    public static function generalActivate(array $info)
    {
        $result = self::getInstance()->card->general_card->activate($info);
        return Wechat::apiError($result);
    }

    /**
     * 通用卡 - 撤销激活
     * @param string $cardId 卡券ID
     * @param string $code CODE
     * @return array|bool|void
     */
    public static function generalDeactivate(string $cardId, string $code)
    {
        $result = self::getInstance()->card->general_card->deactivate($cardId, $code);
        return Wechat::apiError($result);
    }

    /**
     * 通用卡 - 更新用户信息
     * @param array $info 用户信息
     * @param array $info[] TODO
     * @return array|bool|void
     */
    public static function generalUpdateUser(array $info)
    {
        $result = self::getInstance()->card->general_card->updateUser($info);
        return Wechat::apiError($result);
    }

    /**
     * 会员卡 - 卡券激活
     * @param array $info 需要激活的会员卡信息
     * @param string $info[membership_number] 会员卡编号
     * @param string $info[code] 创建会员卡时获取的初始code
     * @param string $info[card_id] 卡券ID, 自定义code卡券必填
     * @param string(128) $info[background_pic_url] 商家自定义会员卡背景图, 须先调用上传图片接口 将背景图上传至CDN, 否则报错, 卡面设计请遵循 微信会员卡自定义背景设计规范 非必填
     * @param integer $info[activate_begin_time] 激活后的有效起始时间, 若不填写默认以创建时的data_info为准, Unix时间戳格式 非必填
     * @param integer $info[activate_end_time] 激活后的有效截至时间, 若不填写默认以创建时的data_info为准, Unix时间戳格式 非必填
     * @param integer $info[init_bonus] 初始积分, 不填为0 非必填
     * @param string(32) $info[init_bonus_record] 积分同步说明 非必填
     * @param string(32) $info[init_balance] 初始余额, 不填为0 非必填
     * @param string(32) $info[init_custom_field_value1] 创建时字段custom_field1定义类型的初始值, 限制为4个汉字, 12字节 非必填
     * @param string(32) $info[init_custom_field_value2] 创建时字段custom_field2定义类型的初始值, 限制为4个汉字, 12字节 非必填
     * @param string(32) $info[init_custom_field_value3] 创建时字段custom_field3定义类型的初始值, 限制为4个汉字, 12字节 非必填
     * @return array|bool|void
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function memberActivate(array $info)
    {
        $result = self::getInstance()->card->member_card->activate($info);
        return Wechat::apiError($result);
    }

    /**
     * 会员卡 - 设置开卡字段
     * @param string $cardId 卡券ID
     * @param array $settings 会员卡激活时的选项
     * @param array $settings 详细见下发文档地址
     * @return array|bool|void
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function memberSetActivationForm(string $cardId, array $settings)
    {
        $result = self::getInstance()->card->member_card->setActivationForm($cardId, $settings);
        return Wechat::apiError($result);
    }

    /**
     * 拉取会员信息
     * @param string $cardId 卡券ID
     * @param string $code CODE
     * @return array|bool|void
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function memberGetUser(string $cardId, string $code)
    {
        $result = self::getInstance()->card->member_card->getUser($cardId, $code);
        return Wechat::apiError($result);
    }

    /**
     * 更新会员信息
     * @param array $info 信息
     * @param array $info 详细见下发文档地址
     * @return array|bool|void
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/card
     */
    public static function memberUpdateUser(array $info)
    {
        $result = self::getInstance()->card->member_card->updateUser($info);
        return Wechat::apiError($result);
    }

}